// pages/shop/shop_category.js
var app = getApp();
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pages: 1,  //获取第几页的数据
    shuaxin: true,
    no_pro: false,
    natureid: 0,
    productData: [],
    open: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    if (!options.natureid) {
      app.toast_up("参数错误");
    } else {
      that.data.natureid = options.natureid;
      wx.setNavigationBarTitle({ title: options.title })
      console.log(that.data.natureid)
      that.setData({ keyword: options.keyword, page: 1 })
      app.getUserInfo(function (res) {
        that.setData({ open: res.openid });
      }, function () {
        that.onLoad(options)
        
      });
      
      //that.setData({ open: "1" });
      that.get_shop(); //获取商品信息
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  get_shop: function () {
    var openid = wx.getStorageSync('openid');
    app.req(app.local_url + "do=shop&p=index&op=goods", { "natureid": that.data.natureid, "page": that.data.pages, "od": openid,'shop_id':wx.getStorageSync('shop_id') }, function (res) {
      console.log(res)
      if (res.status == 1) {

        if (that.data.productData.length == 0 && res.result.goods == '') {
          that.setData({ productData: that.data.recommend_shop, no_pro: true }); return;
        } else { that.setData({ no_pro: false }); }


        if (res.result.goods == '') { app.toast_up("无更多数据"); return }//商品加载完了

        var p = that.data.productData.concat(res.result.goods)   //增加刷新的商品


        that.setData({ productData: p, pages: that.data.pages + 1, imgUrls: res.result.advs })

        setTimeout(function () { that.setData({ shuaxin: true }) }, 500)

      } else if (res.status == 0) {
        app.toast_up(res.result)
      } else {
        app.modal("错误提示-1", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_shop()
          }
        })
      }
    })
  },
  inputchange: function (e) {
    // console.log(e.detail.value)
    that.setData({ keyword: e.detail.value.trim(), pages: 1 })
  },
  // onPullDownRefresh: function () {
  //   that.setData({ keyword: '', pages: 1, shuaxin: true, no_pro: false, });
  //   wx.showNavigationBarLoading() //在标题栏中显示加载
  //   setTimeout(function () {
  //     that.setData({ productData: [] });
  //     that.get_shop(); //下拉刷新
  //     wx.hideNavigationBarLoading() //完成停止加载
  //     wx.stopPullDownRefresh() //停止下拉刷新
  //   }, 1500);
  // },
  // onReachBottom: function () {
  //   console.log("滚到底了" + that.data.shuaxin);
  //   if (that.data.shuaxin) {
  //     that.setData({ shuaxin: false })
  //     that.get_shop();
  //   }
  // },
  tobottom: function () {
    if (that.data.shuaxin) {
      that.setData({ shuaxin: false })
      that.get_shop();
    }
  },
  select_shop: function () {
    if (that.data.search_ss == "block") {
      that.search_start();
    } else {
      if (that.data.keyword != "") {
        that.setData({ pages: 1 })
        that.get_shop()
      } else {
        app.toast_up("无搜索内容!");
      }
    }
  },
  delete_put: function () {
    this.setData({ keyword: "" });
  },
  quxiao: function () {
    wx.switchTab({
      url: './shop',
    })
  }
})